package com.herui.iot.ui.mime;

import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sport_care.BasePresenter;
import com.herui.sport_care.BaseView;
import com.herui.sport_care.data.BaseCallBack;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.user.Gender;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.data.user.source.UserRepository;
import com.herui.sport_care.data.user.source.UserSource;
import com.herui.sport_care.request.SportResponse;

/**
 * Created by amanda.shan on 2022/12/2.
 */
public class UserPresenter implements BasePresenter {

    public static interface UserView extends BaseView{
        default void onLoginSucess(){}
        // 修改用户信息
        default void verify(boolean flag){}
        // 修改用户信息
        default void onModify(boolean flag){}
        // 获取手机验证码
        default void onCode(boolean flag){}
        // 修改手机号
        default void onModifyPhone(boolean flag){}
        // 修改密码
        default void onChangePassword(boolean flag){}
        // 登出
        default void onLoginOut(){}
    }

    private UserSource userSource;
    private UserView userView;

    public UserPresenter(UserView userView) {
        this.userSource = RepositoryFactory.getInstance().getUserSource();
        this.userView = userView;
    }

    public void login(String account, String password){
        userSource.login(account, password, new Callback.CommonCallback<UserInfo>() {
            @Override
            public void onSuccess(UserInfo result) {
                userView.onLoginSucess();
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取用户信息
     * @return
     */
    public UserInfo getUserInfo(){
        return userSource.getUserInfo();
    }

    /**
     * 修改用户信息
     * @param name
     * @param birthday
     * @param gender
     */
    public void modifyUserInfo(String name, String birthday, Gender gender){
        userSource.modifyUserInfo(name, birthday, gender, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.onModify(true);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
                userView.onModify(false);
            }
        });
    }

    /**
     * 验证token
     */
    public void verify(){
        userSource.verify(new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.verify(true);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
//                processError(e, code, message);
                userView.verify(false);
            }
        });
    }

    /**
     * 获取手机验证码
     * @param mobile
     */
    public void getCode(String mobile){
        userSource.getCode(mobile, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.onCode(true);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
                userView.onCode(false);
            }
        });
    }

    /**
     * 修改手机号
     * @param mobile
     * @param code
     */
    public void modifyPhone(String mobile, String code){
        userSource.modifyPhone(mobile, code, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.onModifyPhone(true);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
                userView.onModifyPhone(false);
            }
        });
    }

    /**
     * 修改密码
     * @param password          修改后的密码
     * @param formerPassword    原密码
     */
    public void changePassword(String password, String formerPassword){
        userSource.changePassword(password, formerPassword, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.onChangePassword(true);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
                userView.onChangePassword(false);
            }
        });
    }

    /**
     * 登出
     */
    public void loginOut(){
        userSource.loginOut(new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                userView.onLoginOut();
            }

            @Override
            public void onError(Throwable e, String code, String message) {
//                processError(e, code, message);
                userView.onLoginOut();
            }
        });
    }

    @Override
    public void start() {

    }

    @Override
    public BaseView getView() {
        return userView;
    }
}
